using Godot;
using System;

using CurrentGame.Utils.Interpolation;


public partial class LightRangeHint : Sprite2D
{
	[Export]
	public float BaseScale { get; set; } = 1.0f;

	[Export]
	public float InterpolationWeight
	{
		get => _interpolation.Weight;
		set => _interpolation.Weight = value;
	}

	private LerpInterpolation _interpolation;
	private float _currentScale = 1.0f, _currentAlpha = 0.0f;

	// Node reference
	private Area2D _lightDetectArea;

	public LightRangeHint()
	{
		_interpolation = new LerpInterpolation(0.01f, new Vector3(-1f, 0.00001f, -1f));
	}


    public override void _Ready()
    {
		_interpolation.Vector2Value = new Vector2(0.0f, BaseScale);  // alpha, scale
		_interpolation.Teleport();
		_currentScale = BaseScale;

        _lightDetectArea = GetNode<Area2D>("../LightDetect");
    }

	public override void _Process(double delta)
	{
		if (_interpolation.Process(delta))
		{
			var current = _interpolation.Vector2Value;
			Modulate = new Color(1.0f, 1.0f, 1.0f, current.X);
			Scale = new Vector2(current.Y, current.Y);
		}
	}


	// Export interfaces
	public void ShowRangeHint()
	{
		_currentAlpha = 1.0f;
        _interpolation.Vector2Value = new Vector2(1.0f, _currentScale);
    }

	public void HideRangeHint()
	{
		_currentAlpha = 0.0f;
		_interpolation.Vector2Value = new Vector2(0.0f, _currentScale);
    }

    public void SetRangeLevel(int level)
	{
		float scale = Mathf.Clamp(level, 0.0f, 3.0f) / 3.0f;
		_lightDetectArea.Scale = new Vector2(scale, scale);

        scale *= BaseScale;
        _currentScale = scale;
		_interpolation.Vector2Value = new Vector2(_currentAlpha, scale);
	}

}
